package com.dotnet.modules.template.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dotnet.common.utils.Constants;
import com.dotnet.common.utils.DateUtils;
import com.dotnet.common.web.BaseController;
import com.dotnet.modules.field.entity.FieldPrice;
import com.dotnet.modules.field.entity.ProductField;
import com.dotnet.modules.field.entity.ProductFieldValue;
import com.dotnet.modules.field.entity.ProductFieldVo;
import com.dotnet.modules.field.entity.ProductVo;
import com.dotnet.modules.field.service.FieldPriceService;
import com.dotnet.modules.field.service.ProductFieldService;
import com.dotnet.modules.field.service.ProductFieldValueService;
import com.dotnet.modules.order.entity.Order;
import com.dotnet.modules.order.service.OrderService;
import com.dotnet.modules.product.entity.ProductInfo;
import com.dotnet.modules.product.service.ProductInfoService;
import com.dotnet.modules.template.entity.ProductTemplate;
import com.dotnet.modules.template.service.ProductTemplateService;

/**
 * 
 * 描述：产品模板
 * 创建人： 魏泽超
 * 创建时间: 2016-5-30 下午5:29:30
 * @version
 */
@Controller
@RequestMapping("/template")
public class ProductTemplateController extends BaseController {
	@Autowired
	private ProductTemplateService templateService;

	@Autowired
	private OrderService orderService;

	@Autowired
	private ProductFieldService fieldService;

	@Autowired
	private ProductInfoService productInfoService;

	@Autowired
	private ProductFieldValueService fieldValueService;

	@Autowired
	private FieldPriceService fieldPriceService;

	/**
	 * 
	 * 描述：显示产品列表
	 * 创建人： 魏泽超
	 * 创建时间: 2016-5-31 上午8:56:09
	 * @version
	 */
	/*
	 * @RequestMapping("/productList") public String productList(ModelMap model,
	 * HttpServletRequest request, Integer pageNo) { // 参数 StringBuilder params
	 * = new StringBuilder(); ProductInfoPageVo productInfo = new
	 * ProductInfoPageVo();
	 * 
	 * // 如果页号为null 或小于1 设为1 productInfo.setPageNo(Pagination.cpn(pageNo));
	 * 
	 * productInfo.setPageSize(Constants.PAGE_SIZE);
	 * 
	 * productInfo.setIsUse(1); productInfo.setIsDelete(0);
	 * 
	 * Pagination pagination = productInfoService.pagelist(productInfo);
	 * 
	 * // 分页展示 String url = request.getContextPath()+"/template/productList.do";
	 * pagination.pageView(url, params.toString());
	 * 
	 * 
	 * //List<ProductInfo> queryAll = productInfoService.queryAll(productInfo);
	 * if (null != pagination) { model.put("page", pagination); } return
	 * "/producttemplate/list"; }
	 */

	/**
	 * 
	 * 描述：跳转到模板添加页面
	 * 创建人： 魏泽超
	 * 创建时间: 2016-5-31 上午8:57:30
	 * @version
	 */
	@RequestMapping("/toAddTemplate")
	public String toAddTemplate(String productId, ModelMap model) {
		model.put("productId", productId);
		ProductTemplate productTemplate = new ProductTemplate();
		productTemplate.setProductId(Integer.valueOf(productId));
		model.put("template", templateService.queryOne(productTemplate));
		return "/producttemplate/form";
	}

	/**
	 * 
	 * 描述：模板内容添加或修改
	 * 创建人： 魏泽超
	 * 创建时间: 2016-5-31 上午10:28:43
	 * @version
	 */
	@ResponseBody
	@RequestMapping("/template")
	public Map<String, Object> template(ProductTemplate productTemplate, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		productTemplate.setCreateTime(DateUtils.getCurrentDate());
		int i = 0;
		if (null == productTemplate.getId()) {
			i = add(productTemplate);
		} else {
			i = update(productTemplate);
		}
		if (i > 0) {
			map.put("status", true);
		}
		return map;
	}

	/**
	 * 
	 * 描述：添加
	 * 创建人： 魏泽超
	 * 创建时间: 2016-5-31 上午10:20:37
	 * @version
	 */
	private int add(ProductTemplate productTemplate) {
		return templateService.addSelective(productTemplate);
	}

	/**
	 * 
	 * 描述：修改
	 * 创建人： 魏泽超
	 * 创建时间: 2016-5-31 上午10:20:43
	 * @version
	 */
	private int update(ProductTemplate productTemplate) {
		return templateService.updateByPrimaryKeySelective(productTemplate);
	}

	
	/**
	 * 去订单填写页面
	 * @param model
	 * @param productId
	 * @return
	 */
	@RequestMapping("/toOrder")
	public String toOrder(ModelMap model, String productId[]) {
		logger.info("---进入showPage方法--");
		// 生成订单号
		String orderNo = createOrderCode();
		String ids[] = productId;
		List<ProductTemplate> templateList = templateService.getTemplateListByProductId(ids);
		if (null != templateList && templateList.size() > 0) {
			model.put("templateList", templateList);
		}
		model.put("orderNo", orderNo);
		return "/order/service";
	}

	/**
	 * 
	 * 描述：生成订单编号
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-6 上午11:20:46
	 * @version
	 */
	private String createOrderCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("MMddmmss");
		Date create_time = new Date();
		Integer num = orderService.queryCount(null);
		// 当天日期+order编码+订单总数
		String orderNo = sdf.format(create_time) + Constants.ORDER_CODE + num(num);
		Order order = new Order();
		order.setOrderNo(orderNo);
		Order queryOne = orderService.queryOne(order);
		if (null != queryOne) {
			orderNo = sdf.format(create_time) + Constants.ORDER_CODE + num(num);
		}
		System.out.println(orderService.queryCount(null));
		System.out.println(orderNo);
		return orderNo;
	}

	/**
	 * 
	 * 描述：
	 * 创建人： 魏泽超
	 * 创建时间: 2016-6-6 下午3:05:24
	 * @version
	 */
	private String num(Integer num) {
		String n = "";
		if (null != num) {
			if (num.toString().length() == 1) {
				n = "00" + num;
			} else if (num.toString().length() == 2) {
				n = "0" + num;
			}
		}
		return n;
	}

	@RequestMapping("/getJson")
	public void getJson(HttpServletResponse response, String productId) {
		logger.info("进入getJson方法");
		ProductField productField = new ProductField();
		ProductInfo productInfo = new ProductInfo();
		productInfo.setId(Integer.valueOf(productId));
		ProductInfo queryOne = productInfoService.queryOne(productInfo);
		productField.setProductId(Integer.valueOf(productId));
		productField.setIsDelete(0);
		productField.setIsUse(1);
		List<ProductField> queryAll = fieldService.queryAll(productField);
		List<ProductFieldVo> productFieldVoList = new ArrayList<ProductFieldVo>();
		// List<ProductFieldValue> pfvQueryAll=new
		// ArrayList<ProductFieldValue>();
		// List<FieldPrice> fpQueryAll=new ArrayList<FieldPrice>();
		// ProductInfoVo productInfoVo = new ProductInfoVo();
		// productInfoVo.setProductId("6");
		if (null != queryAll && queryAll.size() > 0) {
			for (ProductField pf : queryAll) {
				ProductFieldVo productFieldVo = new ProductFieldVo();
				productFieldVo.setFieldValueList(new ArrayList<ProductFieldValue>());
				productFieldVo.setFieldPriceList(new ArrayList<FieldPrice>());
				productFieldVo.setProductType(queryOne.getProductType().toString());
				createEntity(productFieldVo, pf);
				ProductFieldValue pfv = new ProductFieldValue();
				pfv.setFieldId(pf.getId());
				pfv.setIsDelete(0);
				pfv.setIsUse(1);
				List<ProductFieldValue> query = fieldValueService.queryAll(pfv);
				if (null != query && query.size() > 0) {
					for (ProductFieldValue productFieldValue : query) {
						if (pf.getId().equals(productFieldValue.getFieldId())) {
							productFieldVo.getFieldValueList().add(productFieldValue);
						}
						// pfvQueryAll.add(productFieldValue);
						FieldPrice fieldPrice = new FieldPrice();
						fieldPrice.setFieldValueId(productFieldValue.getId());
						fieldPrice.setIsDelete(0);
						fieldPrice.setIsUse(1);
						List<FieldPrice> queryAll2 = fieldPriceService.queryAll(fieldPrice);
						if (null != queryAll2 && queryAll2.size() > 0) {
							for (FieldPrice fieldPrice2 : queryAll2) {
								if (fieldPrice2.getFieldValueId().equals(productFieldValue.getId())) {
									productFieldVo.getFieldPriceList().add(fieldPrice2);
								}
								// fpQueryAll.add(fieldPrice2);
							}
						}
					}
				}
				productFieldVoList.add(productFieldVo);
			}
		}
		/*
		 * ProductVo productVo=new ProductVo();
		 * productVo.setFieldList(queryAll);
		 * productVo.setFeildValueList(pfvQueryAll);
		 * productVo.setFieldPriceList(fpQueryAll);
		 */
		// productInfoVo.setProductField(queryAll);
		// productInfoVo.setProductFieldValue(pfvQueryAll);
		// productInfoVo.setFieldPrice(fpQueryAll);
		// Object[]
		// obj={productInfoVo.getProductField(),productInfoVo.getProductFieldValue(),productInfoVo.getFieldPrice()};
		printJSON(response, productFieldVoList);
		// printJSON(response,obj);
	}

	private void createEntity(ProductFieldVo productFieldVo, ProductField pf) {
		productFieldVo.setId(pf.getId());
		productFieldVo.setCreateTime(pf.getCreateTime());
		productFieldVo.setFieldName(pf.getFieldName());
		productFieldVo.setFieldType(pf.getFieldType());
		productFieldVo.setInputName(pf.getInputName());
		productFieldVo.setIsDelete(pf.getIsDelete());
		productFieldVo.setIsUse(pf.getIsUse());
		productFieldVo.setProductId(pf.getProductId());
		productFieldVo.setProductName(pf.getProductName());
		productFieldVo.setIsSum(pf.getIsSum());
		if (null != pf.getIsSum() && 1 == pf.getIsSum()) {
			productFieldVo.setIsSpe(pf.getIsSpe() == null ? 0 : pf.getIsSpe());
		} else {
			productFieldVo.setIsSpe(0);
		}
		productFieldVo.setIsCheckboxChild(pf.getIsCheckboxChild());
		productFieldVo.setIsValidate(pf.getIsValidate());
		if (null != pf.getIsValidate() && 1 == pf.getIsValidate()) {
			productFieldVo.setErrorMsg(pf.getErrorMsg());
			productFieldVo.setInputMsg(pf.getInputMsg());
			productFieldVo.setValidateType(pf.getValidateType());
		}
	}
}
